package com.yixinsoft.basic.web.filter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.PathMatchingFilter;
import org.springframework.beans.factory.annotation.Autowired;

import com.yixinsoft.basic.common.Constants;
import com.yixinsoft.basic.model.manage.system.User;
import com.yixinsoft.basic.service.manage.system.UserService;

/**
 * Created by hqy on 2016/06/09
 */
public class SysUserFilter extends PathMatchingFilter {
	
	@Autowired
	private UserService userService;
	
	@Override
	protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
		String username = (String)SecurityUtils.getSubject().getPrincipal();
		if (username != null) {
			User user = userService.findByUsername(username);
			request.setAttribute(Constants.CURRENT_USER, user);
		}
		return true;
	}

}
